package com.isunland.managesystem.utils;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.SimpleReturnResponse;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/6/24 10:30
 */

public class MenuUtil {
  public static MenuItem addSubMenu(Menu menu, int menuId, int resId) {
    if (menu == null) return null;
    MenuItem menuItem = menu.add(Menu.NONE, menuId, 0, resId);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
    return menuItem;
  }

  public static void addReadReceiptMenu(final BaseVolleyActivity context, Menu menu,
      final String readReceipt, final String id) {
    if (context == null || menu == null || id == null) return;
    menu.add("回执").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        final EditText etReceipt = new EditText(context);
        etReceipt.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
        etReceipt.setPadding(16, 16, 16, 16);
        etReceipt.setLines(3);
        etReceipt.setHint(R.string.hintInput);
        etReceipt.setText(readReceipt);
        new AlertDialog.Builder(context).setTitle("已读回执")
            .setView(etReceipt)
            .setPositiveButton(R.string.submit, new DialogInterface.OnClickListener() {
              @Override public void onClick(DialogInterface dialog, int which) {
                String receiptStr = MyStringUtil.getText(etReceipt);
                if (MyStringUtil.isEmpty(receiptStr)) return;
                String url = ApiConst.getAbsoluteUrl(ApiConst.URL_UPDATE_READ_RECEIPT);
                ParamsNotEmpty params = new ParamsNotEmpty();
                params.put("mainId", id);
                params.put("readReceipt", receiptStr);
                context.volleyPost(url, params.getParams(), new SimpleReturnResponse(context));
              }
            })
            .setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
              @Override public void onClick(DialogInterface dialog, int which) {

              }
            })
            .show();
        return true;
      }
    }).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
  }
}
